package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jx.dao.CatDao;
import com.jx.dao.ProductDao;
import com.jx.daoimpl.CatDaoImpl;
import com.jx.daoimpl.ProductDaoImpl;
import com.jx.model.Product;
import com.jx.service.CatService;
import com.jx.service.ProService;
import com.jx.servicrimpl.CatServiceImpl;
import com.jx.servicrimpl.ProServiceImpl;

public class AddProController extends HttpServlet{
	//定义ProService类型引用proService指向ProServiceImpl对象以调用dao层方法
		ProService proService = new ProServiceImpl();
		ProductDao prodao = new ProductDaoImpl();
		CatDao catdao = new CatDaoImpl();
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doPost(req, resp);
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置字符集
			req.setCharacterEncoding("UTF8");
			resp.setCharacterEncoding("UTF8");
			//创建流对象
			PrintWriter pw = resp.getWriter();
			//创建session对象
			HttpSession session = req.getSession();
			session.setAttribute("category", catdao.showCatrgory(0, 50));
			if (req.getParameter("pname")==""||req.getParameter("pprice")==""||req.getParameter("pintroduce")==""||req.getParameter("ppicture")==""||req.getParameter("pcount")==""||req.getParameter("pstate")==""||req.getParameter("cname")==""	) {
				pw.write("0");
			}else{
			//获取前台传来的数据
			String pname = req.getParameter("pname");
			double pprice = Double.parseDouble(req.getParameter("pprice"));
			String pintroduce = req.getParameter("pintroduce");
			String ppicture = req.getParameter("ppicture");
			int pcount =Integer.parseInt(req.getParameter("pcount"));
			String pmodel = req.getParameter("pmodel");
			String psize = req.getParameter("psize");
			String pcolor = req.getParameter("pcolor");
			int pstate = Integer.parseInt(req.getParameter("pstate"));
			String cname = req.getParameter("cname");
			int cid = prodao.selectCidByCname(cname);
			//实例化商品对象
			Product p = new Product(pname,pprice,pintroduce,ppicture,pcount,pmodel,psize,pcolor,pstate,cid);
			if (proService.addProductService(p)) {
				pw.write("1");
			}
			
			}
			pw.flush();
			pw.close();
		}
}
